import {
    get
} from "../../utils/db"
const app = getApp()
const db = wx.cloud.database()
Page({
    data: {
        userInfo: {},
        isLogin: false,
        typeImg: [
            "../../imgs/index_07.jpg",
            "../../imgs/index_09.jpg",
            "../../imgs/index_11.jpg"
        ],
        type: [],
        recipes: [],
        page: 1,
        pageSize: 10,
    },
    onLoad() {
        // 获取菜谱分类
        db.collection('type')
            .limit(3).get()
            .then(res => {
                this.setData({
                    type: res.data
                })
            })
            .catch(err => {
                console.log(err);
            })
        // 获取用户信息
        let userInfo = app.globalData.userInfo
        if (userInfo == null) {
            this.setData({
                userInfo
            })
            app.getInfo = res => {
                this.setData({
                    userInfo: res
                })
            }
        } else {
            this.setData({
                userInfo
            })
        }
        this.getHotMenu()
    },
    //触底
    onReachBottom() {
        this.data.page += 1;
        this.getHotMenu()
    },
    toList() {
        wx.navigateTo({
            url: '/pages/type/type',
        })
    },
    myClick(e) {
        wx.navigateTo({
            url: '/pages/list/list?id=' + e.currentTarget.id + '&name=' + e.currentTarget.dataset.name,
        })
    },
    // 获取热门菜谱
    async getHotMenu() {
        // let res = await get("menu").catch(err => {
        //     console.log(err);
        //     return;
        // })
        // let recipes = res.data.slice().sort((a, b) => {
        //     return b.addtime - a.addtime
        // })
        // this.setData({
        //     recipes
        // })
        var page = this.data.page; //第几页
        var pageSize = this.data.pageSize; //每一页显示几条
        var _skip = (page - 1) * pageSize; //跳过多少条

        var result = await db.collection("menu").skip(_skip).limit(pageSize).orderBy("addtime", "desc").get()
            .catch(err => console.error(err))
        this.setData({
            recipes: this.data.recipes.concat(result.data)
        })
    },
    toDetail(e) {
        let id = e.currentTarget.id
        wx.navigateTo({
            url: '/pages/detail/detail?id=' + id,
        })
    },
    toLikes() {
        let openid = wx.getStorageSync('openid')
        wx.navigateTo({
            url: '/pages/list/list?openid=' + openid + '&name=' + "关注菜谱",
        })
    }
})